package xworker.javafx.scene.web;

import javafx.scene.web.PopupFeatures;
import javafx.scene.web.PromptData;
import javafx.scene.web.WebEngine;
import javafx.util.Callback;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.util.JavaFXUtils;

import java.io.File;

public class WebEngineActions {
    static{
        PropertyFactory.regist(WebEngine.class, "userDataDirectoryProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.userDataDirectoryProperty();
        });
        PropertyFactory.regist(WebEngine.class, "onStatusChangedProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.onStatusChangedProperty();
        });
        PropertyFactory.regist(WebEngine.class, "createPopupHandlerProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.createPopupHandlerProperty();
        });
        PropertyFactory.regist(WebEngine.class, "confirmHandlerProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.confirmHandlerProperty();
        });
        PropertyFactory.regist(WebEngine.class, "promptHandlerProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.promptHandlerProperty();
        });
        PropertyFactory.regist(WebEngine.class, "onVisibilityChangedProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.onVisibilityChangedProperty();
        });
        PropertyFactory.regist(WebEngine.class, "javaScriptEnabledProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.javaScriptEnabledProperty();
        });
        PropertyFactory.regist(WebEngine.class, "userStyleSheetLocationProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.userStyleSheetLocationProperty();
        });
        PropertyFactory.regist(WebEngine.class, "onAlertProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.onAlertProperty();
        });
        PropertyFactory.regist(WebEngine.class, "onResizedProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.onResizedProperty();
        });
        PropertyFactory.regist(WebEngine.class, "userAgentProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.userAgentProperty();
        });
        PropertyFactory.regist(WebEngine.class, "onErrorProperty", o -> {
            WebEngine obj = (WebEngine) o;
            return obj.onErrorProperty();
        });
    }

    public static void init(WebEngine node, Thing thing, ActionContext actionContext){
        if(thing.valueExists("confirmHandler")){
            Callback<String,Boolean> confirmHandler = JavaFXUtils.getObject(thing, "confirmHandler", actionContext);
            if(confirmHandler != null) {
                node.setConfirmHandler(confirmHandler);
            }
        }
        if(thing.valueExists("createPopupHandler")){
            Callback<PopupFeatures,WebEngine> createPopupHandler = JavaFXUtils.getObject(thing, "createPopupHandler", actionContext);
            if(createPopupHandler != null) {
                node.setCreatePopupHandler(createPopupHandler);
            }
        }
        if(thing.valueExists("javaScriptEnabled")){
            node.setJavaScriptEnabled(thing.getBoolean("javaScriptEnabled"));
        }
        if(thing.valueExists("promptHandler")){
            Callback<PromptData,String> promptHandler = JavaFXUtils.getObject(thing, "promptHandler", actionContext);
            if(promptHandler != null){
                node.setPromptHandler(promptHandler);
            }
        }
        if(thing.valueExists("userAgent")){
            node.setUserAgent(thing.getString("userAgent"));
        }
        if(thing.valueExists("userDataDirectory")){
            File userDataDirectory = thing.doAction("getUserDataDirectory", actionContext);
            if(userDataDirectory != null) {
                node.setUserDataDirectory(userDataDirectory);
            }
        }
        if(thing.valueExists("userStyleSheetLocation")){
            node.setUserStyleSheetLocation(thing.getString("userStyleSheetLocation"));
        }
    }
}
